package com.example.controller;

import com.jcraft.jsch.SftpException;
import javax.servlet.http.HttpServletResponse;

import com.example.utils.FileUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FileController {
    //先将用户的图片存储到本地在发送到服务器

    @PostMapping("/upload")
    @CrossOrigin(origins="*")  // 允许所有源进行跨域请求
    public String upload(MultipartFile file) throws IOException, SftpException {
        String url = FileUtil.upload(file);
        return url;
    }


    @GetMapping("/download")
    public void download(String filePath, HttpServletResponse response) throws SftpException, IOException {       //参数为服务器中文件的路径
        FileUtil.download(filePath,response);
    }
}
